<?php

include_once("../info_struct.php");

/*
 Helper Function for zmath_get;
get_content_by_name($content, $search);
parameter:
content - the whole content of the div in which should be searched
search - the key to find the searched content after

return:
string - the searched string
false - if search is not found
*/

function get_content_by_name($content, $search) {
    $find = 'class="'.$search.'"';
    $pos = strpos($content, $find);

    if ($pos === false) {
        echo "Error with finding the class";
        return false;
    } else {
        $offset = strpos($content, "</div>", $pos);
        if ($offset === false) {
            echo "Error with finding the end div";
            return false;
        } else {
            $str = substr($content, $pos + strlen($find) + 1, $offset - ($pos + strlen($find) + 1));
            return preg_replace("/<[^>]*>/","",$str);
        }
    }
    return false;
}

/*
 Usage:
old_zmath_get_by_id(id);
obsolete cause of xml use
paramter:
id - ZMathId of the article

return:
class infos
see info_struct.php for content of the class
false - if all went wrong / id not found
*/

function old_zmath_get_by_id($id) {

    if(function_exists('curl_version')) {
        include_once("../helper_funcs.php");
        $content = curl_file_get_contents("https://zbmath.org/?q=an:".$id);
    } else {
        $content = file_get_contents("https://zbmath.org/?q=an:".$id);
    }
    if ($content === false)
        return false;

    $pos = strpos($content, "<article");
    if ($pos === false) {
        echo "Error with finding the article";
        return false;
    } else {
        $offset = strpos($content, "</article>", $pos);
        if ($offset === false) {
            echo "Error with finding the end article";
            return false;
        } else {
            $length = $offset - $pos;
            $content = substr($content, $pos, $length);
        }
    }

    $return = new infos();
    $return->title = get_content_by_name($content, "title");
    $return->author = get_content_by_name($content, "author");
    $return->source = get_content_by_name($content, "source");
    $return->abstract = get_content_by_name($content, "abstract");
    $return->classification = get_content_by_name($content, "classification");

    $regex_year = array();
    if (preg_match("/\(([0-9]*?)\)/ism", $return->source, $regex_year))
        $return->year = $regex_year[1];

    $pos = strpos($return->title, "Zbl");
    if ($pos !== false)
        $return->zmath = substr($return->title, $pos+4);

    $return->from = "zma";

    return $return;
}

/*
 Usage:
zmath_get_by_id(id);
paramter:
id - ZMathId of the article

return:
class infos
see info_struct.php for content of the class
false - if all went wrong / id not found
*/

function zmath_get_by_id($id) {

    if(function_exists('curl_version')) {
        include_once("../helper_funcs.php");
        $content = curl_file_get_contents("http://www.zentralblatt-math.org/MIRROR/zmath/en/advanced/zmath.xml?q=an:".$id."&type=xml&format=complete");
    } else {
        $content = file_get_contents("http://www.zentralblatt-math.org/MIRROR/zmath/en/advanced/zmath.xml?q=an:".$id."&type=xml&format=complete");
    }
    if ($content === False) {
        return old_zmath_get_by_id($id);
    }

    $xml = simplexml_load_string($content);
    if ($xml === false)
        return false;

    $return = new infos();

    $entry = $xml->answers->rec;

    $return->year = (string)$entry->py;

    $return->source = preg_replace("/\n/", "", (string)$entry->so);
    $return->journal = preg_replace("/\n/", "", (string)$entry->so);

    $return->author = preg_replace("/\n/", "", (string)$entry->au);

    $return->title = preg_replace("/\n/", "", (string)$entry->ti);

    $return->abstract = preg_replace("/\n/", "", (string)$entry->ab);

    $return->zmath = $id;

    $return->from = "zma";

    return $return;
}

/*
 Usage:
zmath_get_by_string(string);
paramter:
string - the search string
specs - the specification for the search - ti for title (default) / any for everything including name of magazines / au for author

return:
class infos
see info_struct.php for content of the class
false - if all went wrong / id not found
*/

function zmath_get_by_string($string, $specs = "ti:") {

    $zmath_specs = array("ti:", "any:", "au:");

    if (!in_array($specs, $zmath_specs)) {
        echo "Error wrong spec";
        return false;
    }

    $search = str_replace(" ", "%20", $string);

    if(function_exists('curl_version')) {
        include_once("../helper_funcs.php");
        $content = curl_file_get_contents("http://www.zentralblatt-math.org/MIRROR/zmath/en/advanced/zmath.xml?q=".$specs."".$search."&type=xml&format=complete");
    } else {
        $content = file_get_contents("http://www.zentralblatt-math.org/MIRROR/zmath/en/advanced/zmath.xml?q=".$specs."".$search."&type=xml&format=complete");
    }
    if ($content === False) {
        return old_zmath_get_by_string($search, $specs);
    }


    $xml = simplexml_load_string($content);
    if ($xml === false)
        return false;

    $return = array();

    foreach($xml->answers->rec as $entry) {
        $new_entry = new infos();

        $new_entry->year = (string)$entry->py;

        $new_entry->source = preg_replace("/\n/", "", (string)$entry->so);
        $new_entry->journal = preg_replace("/\n/", "", (string)$entry->so);

        $new_entry->author = preg_replace("/\n/", "", (string)$entry->au);

        $new_entry->title = preg_replace("/\n/", "", (string)$entry->ti);

        $new_entry->abstract = preg_replace("/\n/", "", (string)$entry->ab);

        $new_entry->zmath = substr(preg_replace("/\n/", "", (string)$entry->an), 4);

        $new_entry->from = "zma";

        array_push($return, $new_entry);
    }

    return $return;
}

/*
 Usage:
old_zmath_get_by_string(string);
obsolete cause of xml use
paramter:
string - the search string
specs - the specification for the search - ti for title (default) / any for everything including name of magazines / au for author

return:
class infos
see info_struct.php for content of the class
false - if all went wrong / id not found
*/

function old_zmath_get_by_string($string, $specs = "ti:") {

    $zmath_specs = array("ti:", "any:", "au:");

    if (!in_array($specs, $zmath_specs)) {
        echo "Error wrong spec";
        return false;
    }

    if(function_exists('curl_version')) {
        include_once("../helper_funcs.php");
        $content = curl_file_get_contents("https://zbmath.org/?q=".$specs.":".$string);
    } else {
        $content = file_get_contents("https://zbmath.org/?q=".$specs.":".$string);
    }
    if ($content === false)
        return false;

    $found_entries = array();

    while (($pos = strpos($content, '<div class="list">')) !== false) {
        $offset = strpos($content, "</article>", $pos);
        if ($offset === false) {
            echo "Error with finding the end article";
            return false;
        } else {
            $length = $offset - $pos;
            array_push($found_entries, substr($content, $pos, $length));
            $content = substr($content, $offset);
        }
    }

    $return = array();

    foreach ($found_entries as $entry) {

        $new_entry = new infos();
        $new_entry->title = get_content_by_name($entry, "title");
        $new_entry->author = get_content_by_name($entry, "author");
        $new_entry->source = get_content_by_name($entry, "source");

        $pos = strpos($entry, "Full Text DOI");
        if ($pos !== false) {
            $pos = strpos($entry, "http://dx.doi.org/");
            if ($pos !== false) {
                $offset = strpos($entry, "\"", $pos);
                if ($offset !== false) {
                    $length = $offset - $pos;
                    $new_entry->doi = substr($entry, $pos, $length);
                }
            }
        }

        $regex_year = array();
        if (preg_match("/\(([0-9]*?)\)/ism", $new_entry->source, $regex_year))
            $new_entry->year = $regex_year[1];

        $pos = strpos($new_entry->title, "Zbl");
        if ($pos !== false)
            $new_entry->zmath = substr($new_entry->title, $pos+4);

        $new_entry->from = "zma";

        array_push($return, $new_entry);
    }

    return $return;
}
?>